<script setup lang="ts">
import { useGlobalStore } from '@/stores/modules/global'
import { useI18n } from 'vue-i18n'
import { type LanguageType } from '@/stores/interface'
import en from 'element-plus/es/locale/lang/en'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import { onMounted } from 'vue'
import { getBrowserLang } from './utils'
import { computed, reactive } from 'vue'
import { useTheme } from './hooks/useTheme'

const globalStore = useGlobalStore()

// init useI18n
const i18n = useI18n()

// init theme
const { initTheme } = useTheme()
initTheme()

onMounted(() => {
  const language = globalStore.language ?? getBrowserLang()
  i18n.locale.value = language
  globalStore.setGlobalState('language', language as LanguageType)
})

// element 国际化
const locale = computed(() => {
  if (globalStore.language == 'zh') return zhCn
  if (globalStore.language == 'en') return en
  return getBrowserLang() == 'zh' ? zhCn : en
})

// element 组件的大小
const assemblySize = computed(() => globalStore.assemblySize)

// element 按钮配置
const buttonConfig = reactive({ autoInsertSpace: false })
</script>

<template>
  <el-config-provider :locale="locale" :size="assemblySize" :button="buttonConfig">
    <router-view></router-view>
  </el-config-provider>
</template>

<style scoped></style>
